//
//  MWXRTabBarController.m
//  XiRan-iOS
//
//  Created by liuyiming on 2019/12/4.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWXRTabBarController.h"
#import "MWNavigationController.h"
#import "MWHomeController.h"
#import "MWShopController.h"
#import "MWTreeSeedController.h"
#import "MWMineController.h"

#import "MWPhoneLoginController.h"

@interface MWXRTabBarController ()

@end

@implementation MWXRTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupChildController];
    [self setupTabBarStyle];
}

- (void)setupTabBarStyle {
    // 正常态文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    [normalAttrs setObject:[UIFont systemFontOfSize:12] forKey:NSForegroundColorAttributeName];
    [normalAttrs setObject:MWHexColor(@"#666666") forKey:NSForegroundColorAttributeName];
    
    // 选中态文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    [selectedAttrs setObject:[UIFont systemFontOfSize:12] forKey:NSForegroundColorAttributeName];
    [selectedAttrs setObject:MWHexColor(@"#333333") forKey:NSForegroundColorAttributeName];
    
    [[UITabBarItem appearance] setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    //    [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, -5)];
    
    // 背景属性
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    [UITabBar appearance].translucent = NO;
}

- (void)setupChildController {
    NSString *homeTitle = [[MWConfigManager manager] objectForKey:@"homepage" defaultValue:@"首页"];
    NSString *shopTitle = [[MWConfigManager manager] objectForKey:@"goods" defaultValue:@"商品"];
    NSString *treeSeedTitle = [[MWConfigManager manager] objectForKey:@"treetype" defaultValue:@"树种"];
    NSString *mineTitle = [[MWConfigManager manager] objectForKey:@"my" defaultValue:@"我的"];
    
    [self addChildController:[[MWHomeController alloc] init] title:homeTitle image:@"tab_home_xr_n" selectedImage:@"tab_home_xr_s"];
    [self addChildController:[[MWShopController alloc] init] title:shopTitle image:@"tab_shop_xr_n" selectedImage:@"tab_shop_xr_s"];
    [self addChildController:[[MWTreeSeedController alloc] init] title:treeSeedTitle image:@"tab_tree_seed_xr_n" selectedImage:@"tab_tree_seed_xr_s"];
    [self addChildController:[[MWMineController alloc] init] title:mineTitle image:@"tab_mine_xr_n" selectedImage:@"tab_mine_xr_s"];
}

- (void)addChildController:(UIViewController *)controller
                     title:(NSString *)title
                     image:(NSString *)image
             selectedImage:(NSString *)selectedImage {
    
    MWNavigationController *navController = [[MWNavigationController alloc] initWithRootViewController:controller];
    navController.tabBarItem.title = title;
    navController.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    navController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [self addChildViewController:navController];
}

@end
